﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRP_Saving_Final
{
    class Feasibility
    {
        public static void check(VRP network, List<Nodes> nodes) //this function check the feasibilities
        {
            bool capcheck = false;
            List<float> route_cap=new List<float>();
            for (int i = 0; i < network.routes.Count(); i++)    // assume list<list>.count will return the number of routes
            {
                float cap = 0;
                if (network.routes[i].Count()!=0)
                {
                    for (int j = 0; j < network.routes[i].Count(); j++) //sum all the nodes' capacity togetheer
                        {
                        
                            List<int> current_route = network.routes[i];
                            cap = cap + network.demandarray[current_route[j]];
                        }
                        route_cap.Add(cap);
                        if (cap > network.capacity)
                        {
                            //network.routes_info = "The routes are over capacity";
                            //Console.WriteLine("The routes are not feasible");
                            capcheck = true;
                            return;
                        }
                        
                }
                if (capcheck == true)     //if at least one route is over capacity, we report that the system has a capacity problem
                {
                    network.routes_info = "Routes under capacity: False";
                }
                else
                {
                    network.routes_info = "Routes under capacity: True";
                }
            }
            network.route_capacity = route_cap;             
            if (network.objective < network.optimal_solution)           //check to see if cost result makes sense
            {
                
                network.objective_info = "Feasible Cost: False";
               // Console.WriteLine("The objective is lower than the optimal!");
                return;
            }
            else   //if cost is more than objective, that is a feasible cost
            {
                network.objective_info = "Feasible Cost: True";
            }

            int counter = 0;
            bool nodein = false;
            List<int> missing=new List<int>();

            for (int i = 1; i < network.numnodes; i++)
            {
                    nodein = false;
                    for (int j = 0; j < network.routes.Count(); j++)
                    {
                        if (network.routes[j].Count() != 0)
                        {
                            foreach (int k in network.routes[j])
                            {
                                if (i == k)
                                {
                                    counter++;
                                    nodein = true;    //node i in the routes
                                }
                            }
                        }
                    }
                    if (!nodein)                
                    {
                        missing.Add(i);
                    }

                    
            }

            network.missingnodes=missing;
            int number = 0;

            foreach (var path in network.routes)
            {
                number = number + path.Count();
            }

            if (number != network.numnodes - 1)   //if there are some missing nodes, we should report it
            {
                network.node_info = "All nodes present: False";
               // Console.WriteLine("The routes don't include all the nodes");
                return;

            }
            else
            {
                network.node_info = "All nodes present: True";
            }

             network.gap = (network.objective - network.optimal_solution) / network.optimal_solution;   //compute the gap between cost and optimal amount


        }
    }
}
